package com.example.su_shi_tan_bas_lib.adapter.ab_list_adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.StringRes;

public class CommonViewHolder {

    private final SparseArray<View> mViews;

    private final int mPosition;

    private final View mConvertView;

    private final Context context;



    private CommonViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.context = context;
        this.mPosition = position;
        this.mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        // setTag
        mConvertView.setTag(this);

    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */

    static CommonViewHolder get(Context context, View convertView,

                                ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {

            return new CommonViewHolder(context, parent, layoutId, position);
        }

        return (CommonViewHolder) convertView.getTag();
    }



    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);

        if (view == null) {

            view = mConvertView.findViewById(viewId);

            mViews.put(viewId, view);

        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */

    public CommonViewHolder setText(int viewId, String text) {

        TextView view = getView(viewId);
        if (text !=null) {

            view.setText(text);
        }

        return this;

    }

    public CommonViewHolder setText(int viewId, @StringRes int text) {

        TextView view = getView(viewId);
        if (view !=null) {
            view.setText(text);
        }

        return this;

    }

    public CommonViewHolder setViewBackgroundResource(int viewId, int BackgroundResourceId){
        getView(viewId).setBackgroundResource(BackgroundResourceId);
        return this;
    }





    /**
     * 为TextView设置字体颜色
     *
     * @param viewId
     * @param color
     * @return
     */
    public CommonViewHolder setTextColor(int viewId, int color) {

        TextView view = getView(viewId);

        view.setTextColor(color);

        return this;

    }

    public CommonViewHolder setViewSelector(int viewId, boolean b) {

        View view = getView(viewId);

        view.setSelected(b);

        return this;

    }


    public CommonViewHolder setTextSiz(int viewId, float siz) {

        TextView view = getView(viewId);

        view.setTextSize(siz);

        return this;

    }


    public CommonViewHolder setTextColor(int viewId, String color, String str) {

        TextView view = getView(viewId);

        view.setTextColor(Color.parseColor(color));

        view.setText(str);
        return this;

    }

    public CommonViewHolder setBadgeView(Context context, boolean isAdd, int viewId) {
        TextView view = getView(viewId);
        view.setText("1");
        view.setVisibility(isAdd ? View.VISIBLE : View.INVISIBLE);
        return this;

    }

    public CommonViewHolder setBadgeView1(Context context, int count, int viewId) {
        TextView view = getView(viewId);
        view.setText(""+count);
        view.setVisibility(count==0 ? View.INVISIBLE : View.VISIBLE);
        return this;

    }





    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public CommonViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        if (drawableId == 0) {
            view.setVisibility(View.GONE);

        } else {
            view.setImageResource(drawableId);
            view.setVisibility(View.VISIBLE);
        }
        return this;
    }



    public void setlayRes(int viewId, int res) {
        View view = getView(viewId);
        view.setBackgroundResource(res);
    }






    public CommonViewHolder setViewVisblies(int viewIdTake, int view) {
        getView(viewIdTake).setVisibility(view);
        return this;
    }

    public CommonViewHolder setObjectTag(int viewIdTake, Object object) {
        getView(viewIdTake).setTag(object);
        return this;
    }

    public int getPosition() {
        return mPosition;
    }


}